<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="任命审核通过" name="first"> </el-tab-pane>
      <el-tab-pane label="任命待审核" name="second"></el-tab-pane>
    </el-tabs>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="学期" prop="schoolYear">
        <el-select
          v-model="queryParams.schoolYear"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="option in schoolYearList"
            :key="option.schoolYearId"
            :label="option.schoolYearId"
            :value="option.schoolYearId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="组织名称" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入组织名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.organ_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <!-- <el-input
          v-model="queryParams.type"
          placeholder="请输入组织类型"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="所属系部" prop="unitCode">
        <el-select
          v-model="queryParams.unitCode"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="(dict,index) in unitNameList"
            :key="index"
            :label="dict.sponsorName"
            :value="dict.sponsorCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classNo">
        <el-input
          v-model="queryParams.classNo"
          placeholder="请输入班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="queryParams.studentName" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生学号" prop="studentId">
        <el-input v-model="queryParams.studentId" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="考核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.orgMemberStatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <!-- <el-input v-model="queryParams.markStatus" clearable @keyup.enter.native="handleQuery" /> -->
      </el-form-item>
      <el-form-item label="考核结果" prop="mark">
        <el-select v-model="queryParams.mark" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.evaluation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <!-- <el-input v-model="queryParams.mark" clearable @keyup.enter.native="handleQuery" /> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-dropdown @command="handleCommand" style="margin-left: 10px">
          <el-button type="primary" size="mini" v-hasPermi="['organ:classOrgan:export']"
            >导出任命信息<i class="el-icon-arrow-down el-icon--right"></i
          ></el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="option in schoolYearList"
            :key="option.schoolYearId" :command="option.schoolYearId"  @click="handleExport">{{option.schoolYearId}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button
          type="primary"
          size="mini"
          @click="handleExportAll"
          style="margin-left: 10px"
          v-hasPermi="['organ:classOrgan:exportAll']"
          >导出学生任职<i class="el-icon--right"></i
        ></el-button>
      </el-form-item>

      <!-- </el-form-item> -->
    </el-form>
    <el-table
      border
      v-loading="loading"
      :data="memberList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="学期" align="center" prop="schoolYear" />
      <el-table-column label="组织名称" align="center" prop="orgName" />
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="系部" align="center" prop="unitName" />
      <el-table-column label="组织类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.organ_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="职位" align="center" prop="positionName" />

      <el-table-column prop="status" label="考核状态" width="150" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.orgMemberStatus" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column prop="mark" label="工作考核结果" width="150" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.evaluation" :value="scope.row.mark" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成员任命信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="学期" prop="schoolYear">
          <!-- <span>{{form.schoolYear}}</span> -->
          <el-select v-model="form.schoolYear" placeholder="请选择" clearable style="width: 240px">
            <el-option
            v-for="option in schoolYearList"
            :key="option.schoolYearId"
            :label="option.schoolYearId"
            :value="option.schoolYearId"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="学生管理员" prop="studentId">
          <el-select
            v-model="form.studentId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="searchLoading"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位名称" prop="positionName">
          <el-select
            v-model="form.positionName"
            placeholder="请选择"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="(dict, index) in positionIdList"
              :key="index"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--查看考核信息对话框 -->
    <el-dialog title="查看工作考核" :visible.sync="markOpen" width="800px" append-to-body>
      <el-form ref="markFrom" :model="markFrom" :rules="rules" label-width="100px">
        <el-form-item label="组织名称" prop="orgName">
          <span>{{ markFrom.orgName }}</span>
        </el-form-item>
        <el-form-item label="学期" prop="schoolYear">
          <span>{{ queryParams.schoolYear }}</span>
        </el-form-item>
        <el-form-item label="姓名" prop="studentName">
          <span>{{ markFrom.studentName }}</span>
        </el-form-item>
        <el-form-item label="所属系部" prop="unitName">
          <span>{{ markFrom.unitName }}</span>
        </el-form-item>
        <el-form-item label="职位" prop="positionName">
          <span>{{ markFrom.positionName }}</span>
        </el-form-item>
        <el-form-item label="工作总结" prop="summary" v-if="writeMark">
          <el-input type="textarea" v-model="markFrom.summary"></el-input>
        </el-form-item>
        <el-form-item label="工作总结" prop="summary" v-if="!writeMark">
          <span>{{ markFrom.summary }}</span>
        </el-form-item>
        <el-form-item label="考核结果" prop="mark" v-if="!writeMark">
          <span>{{ markFrom.mark }}</span>
        </el-form-item>
      </el-form>

      <!-- <el-descriptions :column="1" title=""  style="font-size: 15px;">
              <el-descriptions-item  label="组织名称">{{markFrom.orgName}}</el-descriptions-item>
              <el-descriptions-item label="学期">{{queryParams.schoolYear}}</el-descriptions-item>
              <el-descriptions-item label="姓名">{{markFrom.studentName}}</el-descriptions-item>
              <el-descriptions-item label="所属系部">{{markFrom.unitName}}</el-descriptions-item>
              <el-descriptions-item label="职位">{{markFrom.positionName}}</el-descriptions-item>
              <el-descriptions-item label="工作总结">{{markFrom.studentName}}</el-descriptions-item>
              <el-descriptions-item label="考核结果">{{markFrom.mark}}</el-descriptions-item>
          </el-descriptions> -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="summary" v-if="writeMark">确 定</el-button>
        <el-button @click="markCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import {
  listMember,
  getMember,
  delMember,
  addMember,
  updateMember,
  summary,
} from '@/api/organ/classOrganMember';
import {GetMemberList,GetYearList} from '@/api/organ/organ';
import {
  listOrg,
  getOrg,
  delOrg,
  addOrg,
  updateOrg,
  getstudentmanager,
  setstudentmanager,
} from '@/api/organ/classOrgan';
import { listSponsor } from "@/api/system/sponsor";
export default {
  name: 'Member',
  dicts: ['sys_show_hide', 'orgMemberStatus', 'markStatus','organ_type','evaluation'],
  computed: {
    ...mapGetters(['name', 'userName']),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成员任命信息表格数据
      memberList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: null,
        studentId: null,
        studentName: null,
        teacherId: null,
        positionId: null,
        positionLevel: null,
        auditTime: null,
        positionName: null,
        status: null,
        schoolYear: null,
        markStatus: null,
        mark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orgId: [{ required: true, message: '组织ID不能为空', trigger: 'blur' }],
      },
      orgForm: {},
      activeName: 'first',
      schoolYearList: [],
      positionIdList: [
        {
          name: '技术部成员',
          code: '1',
        },
      ],
      unitNameList:[],
      markOpen: false,
      searchLoading: false,
      options: [],
      markFrom: {},
      writeMark: false,
    };
  },
  created() {
    GetYearList().then(response => { 
      this.schoolYearList = response.rows;
    });
     /** 系部下拉框 */
    listSponsor().then(response => {
      this.unitNameList = response.rows;
    });
    this.getList();
  },
  methods: {
    /** 查询成员任命信息列表 */
    getList() {
      // this.queryParams.teacherId = this.name;
      this.loading = true;
      GetMemberList(this.queryParams).then(response => {
        this.memberList = response.rows;
        console.log(this.memberList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    markCancel() {
      this.markOpen = false;
    },
    // 表单重置
    reset() {
      this.form = {
        orgMemberId: null,
        orgId: null,
        studentId: null,
        studentName: null,
        teacherId: null,
        createTime: null,
        createBy: null,
        positionId: null,
        positionLevel: null,
        auditTime: null,
        positionName: null,
        status: '0',
        schoolYear: null,
        markStatus: '0',
        mark: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orgMemberId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加' + this.orgForm.orgName + '任命';
      this.form.schoolYear = this.queryParams.schoolYear;
    },
    /**查看考核按钮操作 */
    markDetail(row) {
      this.writeMark = false;
      this.markOpen = true;
      this.markFrom = row;
      console.log(this.markFrom);
    },
    /**查看填写工作日志操作 */
    writeMarkHandle(row) {
      this.writeMark = true;
      this.markOpen = true;
      this.markFrom = row;
      console.log(this.markFrom);
    },
    summary() {
      summary(this.markFrom).then(response => {
        this.markOpen = false;
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orgMemberId = row.orgMemberId || this.ids;
      getMember(orgMemberId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改成员任命信息';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.orgId = this.orgForm.orgId;
          addMember(this.form).then(response => {
            this.$modal.msgSuccess('操作成功');
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orgMemberIds = row.orgMemberId || this.ids;
      this.$modal
        .confirm('是否确认删除成员为"' + row.studentName + '"的数据项？')
        .then(function () {
          return delMember(orgMemberIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'committee/member/export',
        {
          ...this.queryParams,
        },
        `member_${new Date().getTime()}.xlsx`
      );
    },
    /** 导出本人管理的学生任职 */
    handleExportAll() {
      this.download(
        'committee/member/exportMyManagement',
        {},
        `MyManage_${new Date().getTime()}.xlsx`
      );
    },
    /** 切换标签卡 */
    handleClick(tab, event) {
      if (tab.index == '0') {
        this.showSearch = true;
      } else {
        // this.showSearch=false;
      }
    },
    /** 查询学生管理员 */
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        getstudentmanager(query).then(response => {
          this.loading = false;
          this.options = response.rows.filter(item => {
            return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });
        });
      } else {
        this.options = [];
      }
    },
    /** 导出
     */
    handleCommand(command) {
      console.log(command)
      this.queryParams.schoolYear = command;
      this.download(
        'committee/org/export2',
        {
          ...this.queryParams,
        },
        `org_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
